import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
import 'package:test2/util/platform.dart';

class PicUtil {
  static String savePic(Uint8List? picBytes) {
    if (picBytes == null || picBytes.isEmpty) {
      return "";
    }
    var directory = Directory(picRootPath);
    if (!directory.existsSync()) {
      directory.createSync(recursive: true);
    }
    String name = DateTime.now().millisecondsSinceEpoch.toString();
    File file = File(directory.path + name + ".jpg");
    file.writeAsBytesSync(picBytes);
    return name + ".jpg";
  }

  static String picRootPath = "";

  static initRootPath() async {
    picRootPath = await getParentDir();
  }

  static Future<String> getParentDir() async {
    if (Platform.isAndroid) {
      return (await getApplicationSupportDirectory()).toString();
    } else if (PlatformUtil.isDesktop()) {
      return "./pic/";
    }

    throw Exception();
  }
}
